<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="com.company.ems.model.Employee" %>
<%@ page import="com.company.ems.model.User" %>
<%@ page import="java.text.NumberFormat" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%
    List<Employee> employees = (List<Employee>) request.getAttribute("employees");
    User user = (User) session.getAttribute("user");
    NumberFormat currencyFormat = NumberFormat.getCurrencyInstance();
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
%>
<html>
<head>
    <title>员工管理</title>
    <link rel="stylesheet" type="text/css" href="../css/style.css">
</head>
<body>
<div class="header">
    <h1>员工管理</h1>
    <div class="user-info">
        欢迎, <%= user != null ? user.getUsername() : "" %>
        | <a href="../logout">退出登录</a>
    </div>
</div>

<div class="menu">
    <a href="../dashboard">返回主页</a>
    <a href="employee?action=new">添加员工</a>
</div>

<div class="content">
    <h2>员工列表</h2>

    <% if (employees != null && !employees.isEmpty()) { %>
    <table>
        <tr>
            <th>ID</th>
            <th>姓名</th>
            <th>部门</th>
            <th>职位</th>
            <th>薪资</th>
            <th>邮箱</th>
            <th>电话</th>
            <th>入职日期</th>
            <th>操作</th>
        </tr>
        <% for (Employee emp : employees) { %>
        <tr>
            <td><%= emp.getId() %></td>
            <td><%= emp.getName() %></td>
            <td><%= emp.getDepartment() %></td>
            <td><%= emp.getPosition() %></td>
            <td><%= currencyFormat.format(emp.getSalary()) %></td>
            <td><%= emp.getEmail() != null ? emp.getEmail() : "-" %></td>
            <td><%= emp.getPhone() != null ? emp.getPhone() : "-" %></td>
            <td><%= dateFormat.format(emp.getHireDate()) %></td>
            <td>
                <a href="employee?action=edit&id=<%= emp.getId() %>">编辑</a>
                <a href="employee?action=delete&id=<%= emp.getId() %>"
                   onclick="return confirm('确定要删除员工【<%= emp.getName() %>】吗？此操作不可恢复！')">删除</a>
            </td>
        </tr>
        <% } %>
    </table>
    <% } else { %>
    <div class="no-data">
        <p>暂无员工数据</p>
        <p><a href="employee?action=new">点击添加第一个员工</a></p>
    </div>
    <% } %>
</div>
</body>
</html>